//创建用户相关小仓库
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { ApiLogin } from '@/api/user/index'
import { loginForm, loginResponseData } from '@/api/user/type'
//引入路由
import { constantRouter } from '@/router/routes'
const useUserStore = defineStore(
  'user',
  () => {
    const menuRoutes = ref(constantRouter) //仓库存储生成菜单需要数组(路由)
    const token = ref('')
    const userLogin = async (data: loginForm) => {
      const res: loginResponseData = await ApiLogin(data)
      // console.log('-----------------', res)
      // 成功 -> 存token
      if (res.code === 200) {
        token.value = res.data.token!
        //
        return 'ok'
      } else {
        // 失败 -> 提示
        return Promise.reject(new Error(res.data.message))
      }
    }
    return {
      token,
      userLogin,
      menuRoutes,
    }
  },
  {
    persist: {
      key: 'guigu-user',
      pick: ['token'],
    },
  },
)

export default useUserStore
